﻿Public Class MainForm

    Dim SaveLocation As String
    Dim QuizDetails As New QuizDetails

    Private Sub Open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Open.Click
        Database.Clear()
        MQFOpen.ShowDialog()
        Try
            Dim stream As New IO.FileStream(MQFOpen.FileName, IO.FileMode.Open)
            Dim comp As New System.IO.Compression.GZipStream(stream, IO.Compression.CompressionMode.Decompress)
            Database.ReadXml(comp)
            comp.Close()
            SaveLocation = MQFOpen.FileName
        Catch ex As Exception
            MsgBox("Error loading:" & vbCr & ex.Message, MsgBoxStyle.OkOnly, "Error")
        End Try
        QuizDetails.UpdateBinding()
    End Sub

    Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
        Try
            QuestionsBindingSource.EndEdit()
            QuestionsBindingNavigator.Update()
            QuizDetails.QuizInfoBindingSource.EndEdit()
            If SaveLocation = "" Then
                MQFSave.ShowDialog()
                SaveLocation = MQFSave.FileName()
            End If
            Dim stream As New IO.FileStream(SaveLocation, IO.FileMode.Create)
            Dim comp As New System.IO.Compression.GZipStream(stream, IO.Compression.CompressionMode.Compress)
            comp.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(Database.GetXml()), 0, System.Text.ASCIIEncoding.ASCII.GetByteCount(Database.GetXml()))
            comp.Close()
        Catch ex As Exception
            MsgBox("Error saving:" & vbCr & ex.Message, MsgBoxStyle.OkOnly, "Error")
        End Try
    End Sub

    Private Sub DataTable1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataTable1BindingNavigatorSaveItem.Click
        Try
            QuestionsBindingSource.EndEdit()
            QuestionsBindingNavigator.Update()
            QuizDetails.QuizInfoBindingSource.EndEdit()
            If SaveLocation = "" Then
                MQFSave.ShowDialog()
                SaveLocation = MQFSave.FileName()
            End If
            Dim stream As New IO.FileStream(SaveLocation, IO.FileMode.Create)
            Dim comp As New System.IO.Compression.GZipStream(stream, IO.Compression.CompressionMode.Compress)
            comp.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(Database.GetXml()), 0, System.Text.ASCIIEncoding.ASCII.GetByteCount(Database.GetXml()))
            comp.Close()
        Catch ex As Exception
            MsgBox("Error saving:" & vbCr & ex.Message, MsgBoxStyle.OkOnly, "Error")
        End Try
    End Sub

    Dim AddingNew As Boolean = False
    Private Sub QuestionsBindingSource_AddingNew(ByVal sender As Object, ByVal e As System.ComponentModel.AddingNewEventArgs) Handles QuestionsBindingSource.AddingNew
        AddingNew = True
    End Sub

    Private Sub QuestionsBindingSource_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles QuestionsBindingSource.PositionChanged
        If AddingNew Then
            CType(QuestionsBindingSource.Current.row, Database.QuestionsRow).IsMultichoice = True
            AddingNew = False
        End If
        QuestionNumberTextBox.Text = QuestionsBindingSource.IndexOf(QuestionsBindingSource.Current) + 1
        Try
            Dim ms As New IO.MemoryStream(Convert.FromBase64String(CType(QuestionsBindingSource.Current.row, Database.QuestionsRow).Image))
            PictureBox1.Image = Image.FromStream(ms)
        Catch
            PictureBox1.Image = Nothing
        End Try
        ImageTextBox.Text = ""
        Try
            If CType(QuestionsBindingSource.Current.row, Database.QuestionsRow).IsMultichoice = True Then
                ALabel.Visible = True
                ATextBox.Visible = True
                BLabel.Visible = True
                BTextBox.Visible = True
                CLabel.Visible = True
                CTextBox.Visible = True
                DLabel.Text = "Answer D:"
                DTextBox.Visible = True
                CorrectTextBox.Visible = False
                CorrectDropDown.Visible = True
            Else
                ALabel.Visible = False
                ATextBox.Visible = False
                BLabel.Visible = False
                BTextBox.Visible = False
                CLabel.Visible = False
                CTextBox.Visible = False
                DLabel.Text = "Separate multiple correct answers with a semi-colon (;)"
                DTextBox.Visible = False
                CorrectTextBox.Visible = True
                CorrectDropDown.Visible = False
            End If
        Catch
        End Try
    End Sub

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
        Database.Clear()
    End Sub

    Private Sub MultichoiceCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MultichoiceCheckBox.CheckedChanged
        If MultichoiceCheckBox.Checked = True Then
            ALabel.Visible = True
            ATextBox.Visible = True
            BLabel.Visible = True
            BTextBox.Visible = True
            CLabel.Visible = True
            CTextBox.Visible = True
            DLabel.Text = "Answer D:"
            DTextBox.Visible = True
            CorrectTextBox.Visible = False
            CorrectDropDown.Visible = True
        Else
            ALabel.Visible = False
            ATextBox.Visible = False
            BLabel.Visible = False
            BTextBox.Visible = False
            CLabel.Visible = False
            CTextBox.Visible = False
            DLabel.Text = "Separate multiple correct answers with a semi-colon (;)"
            DTextBox.Visible = False
            CorrectTextBox.Visible = True
            CorrectDropDown.Visible = False
        End If
    End Sub

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        PictureBox1.Image = Nothing
        ImageTextBox.Text = ""
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ImageOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            ImageTextBox.Text = ImageOpenFileDialog.FileName
        End If
    End Sub

    Private Sub ImageTextBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ImageTextBox.DragDrop
        ImageTextBox.Text = e.Data.GetData(GetType(String))
    End Sub

    Private Sub ImageTextBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ImageTextBox.DragEnter
        e.Effect = DragDropEffects.Copy
    End Sub

    Private Sub ImageTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImageTextBox.TextChanged
        Cursor = Cursors.WaitCursor
        Dim image As Image = Nothing
        Try
            image = image.FromFile(ImageTextBox.Text)
        Catch
            If ImageTextBox.Text.StartsWith("http://", True, Globalization.CultureInfo.CurrentCulture) Or ImageTextBox.Text.StartsWith("ftp://", True, Globalization.CultureInfo.CurrentCulture) Then
                Dim WC As New Net.WebClient
                Try
                    image = image.FromStream(WC.OpenRead(ImageTextBox.Text))
                Catch
                    Dim creds As New Net.NetworkCredential
                    creds.UserName = InputBox("Proxy User Name Required")
                    creds.Password = InputBox("Proxy Password Required")
                    WC.Proxy.Credentials = creds
                End Try
            Else : image = Nothing
            End If
        End Try
        PictureBox1.Image = image
        Try
            Dim ms As New IO.MemoryStream
            image.Save(ms, Imaging.ImageFormat.Gif)
            CType(QuestionsBindingSource.Current.row, Database.QuestionsRow).Image = Convert.ToBase64String(ms.ToArray)
            ms.Close()
        Catch
        End Try
        Cursor = Cursors.Default
    End Sub

    Private Sub RemoveImageButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveImageButton.Click
        CType(QuestionsBindingSource.Current.row, Database.QuestionsRow).SetImageNull()
        PictureBox1.Image = Nothing
    End Sub

    Private Sub XMLFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XMLFileToolStripMenuItem.Click
        If Not XMLOpen.ShowDialog = Windows.Forms.DialogResult.OK Then
            Exit Sub
        End If
        Try
            Database.ReadXml(XMLOpen.FileName)
        Catch ex As Exception
            MsgBox("Error loading:" & vbCr & ex.Message, MsgBoxStyle.OkOnly, "Error")
        End Try
    End Sub

    Private Sub XMLFileToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XMLFileToolStripMenuItem1.Click
        If XMLSave.ShowDialog = Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        End If
        Dim Result As DialogResult = MsgBox("Would you like to remove images?" & vbCr & "Not doing so will result in a large file size", MsgBoxStyle.YesNoCancel)
        If Result = Windows.Forms.DialogResult.Yes Then
            Dim NewDatabase As Database = Database.Copy
            For Each QuestionsRow As Database.QuestionsRow In NewDatabase.Questions.Rows
                QuestionsRow.SetImageNull()
            Next
            NewDatabase.WriteXml(XMLSave.FileName)
        ElseIf Result = Windows.Forms.DialogResult.No Then
            Database.WriteXml(XMLSave.FileName)
        End If
    End Sub

    Private Sub PasteImageFromClipboardToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteImageFromClipboardToolStripMenuItem.Click
        Cursor = Cursors.WaitCursor
        Dim Image As Image = My.Computer.Clipboard.GetImage()
        PictureBox1.Image = Image
        Try
            Dim ms As New IO.MemoryStream
            Image.Save(ms, Imaging.ImageFormat.Gif)
            CType(QuestionsBindingSource.Current.row, Database.QuestionsRow).Image = Convert.ToBase64String(ms.ToArray)
            ms.Close()
        Catch
        End Try
        Cursor = Cursors.Default
    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        SaveLocation = ""
        Save.PerformClick()
    End Sub

    Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Select Case MsgBox("Would you like to save before closing?", MsgBoxStyle.YesNoCancel, "Quiz Builder")
            Case MsgBoxResult.Yes
                Save.PerformClick()
            Case MsgBoxResult.No

            Case MsgBoxResult.Cancel
                e.Cancel = True
        End Select
    End Sub

    Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If My.Application.CommandLineArgs.Count > 0 Then
            Database.Clear()
            Try
                Dim stream As New IO.FileStream(My.Application.CommandLineArgs(0), IO.FileMode.Open)
                Dim comp As New System.IO.Compression.GZipStream(stream, IO.Compression.CompressionMode.Decompress)
                Database.ReadXml(comp)
                comp.Close()
                SaveLocation = MQFOpen.FileName
            Catch ex As Exception
                MsgBox("Error loading:" & vbCr & ex.Message, MsgBoxStyle.OkOnly, "Error")
            End Try
        End If
        If Database.QuizInfo.Count = 0 Then
            QuizDetails.Show()
            QuizDetails.Location = New Point(Location.X + Width, Location.Y + 3)
            QuizDetails.QuizInfoBindingSource.DataSource = Database
            QuizDetails.QuizInfoBindingSource.ResetBindings(True)
        End If
    End Sub

    Private Sub MainForm_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        QuizDetails.Location = New Point(Location.X + Width, Location.Y + 3)
    End Sub

    Private Sub ShowQuizDetailsDialogueToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowQuizDetailsDialogueToolStripMenuItem.Click
        If QuizDetails.Visible = True Then
            QuizDetails.BringToFront()
        Else
            QuizDetails = New QuizDetails
            QuizDetails.Show()
            QuizDetails.Location = New Point(Location.X + Width, Location.Y + 3)
            QuizDetails.QuizInfoBindingSource.DataSource = Database
            QuizDetails.QuizInfoBindingSource.ResetBindings(True)
        End If
    End Sub
End Class
